package com.way.Java多线程编程核心技术v2.chapter02_对象及变量的并发访问._2_2_synchronized同步语句块.多个锁就是异步执行;

/**
 * @PROJECT_NAME: my-projects
 * @DESCRIPTION:
 * @USER: 魏彪
 * @DATE: 2023/11/29 14:30
 */
public class Service {

    private String usernameParam;
    private String passwordParam;

    public void setUsernamePassword(String username, String password) {
        try {
            // 此处是两个锁，会造成异步的情况
            String anyString = new String();
            synchronized (anyString) {
                System.out.println("线程名称为：" + Thread.currentThread().getName() + "在"
                        + System.currentTimeMillis() + "进入同步块");
                usernameParam = username;
                Thread.sleep(3000);//模拟处理数据需要的耗时
                passwordParam = password;
                System.out.println("线程名称为：" + Thread.currentThread().getName() + "在"
                        + System.currentTimeMillis() + "离开同步块");
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
